package ru.neoflex.chugunov;

import java.util.ArrayList;
import java.util.Collections;

import ru.neoflex.chugunov.database.WordJDBCDAO;
import ru.neoflex.chugunov.utils.TranslationService;

public class ScanServices {
	WordJDBCDAO db = new WordJDBCDAO();

	public Scan sortTopWords(Scan scan) {
		ArrayList<Word> tmp = new ArrayList<>(scan.getDict());
		for (Word w : scan.getDict()) {
			if (db.chkValue(w.getWord())) {
				tmp.remove(w);
			}
		}
		Collections.sort(tmp);
		Collections.reverse(tmp);
		if (tmp.size() > 200) {
			scan.setDict(tmp.subList(0, 200));
		} else {
			scan.setDict(tmp);
		}
		return scan;
	}

	public void addScan(Scan scan) {
		for (Word w : scan.getDict())
			db.addValue(w);
	}

	public void addScanTranslate(Scan scan) {
		TranslationService ts = new TranslationService();
		for (Word w : scan.getDict()) {
			ts.wordTranslate(w);
			db.updateTranslation(w);
		}
	}
}
